package com.weblog.search.controller;

import com.weblog.search.pojo.Article;
import com.weblog.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.ResultMessage;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author MaoLin Wang
 * @date 2019/9/2817:00
 */
@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {

    @Autowired
    private ArticleService service;

    @Autowired
    private ArticleService articleService;


    /**
     * 分页高亮关键字查询(前台)
     * @param
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value="/search/web/{page}/{size}/key",method= RequestMethod.POST)
    public Result searchHighlightPage(@PathVariable int page, @PathVariable int size, @RequestBody Map<String,String> map){
        String key=map.get("key");
        PageResult pageResult = articleService.searchHighlightPage(page, size,key);
        return  new Result(true, StatusCode.OK,"查询成功",pageResult);
    }
    /**
     * 分页查询(前台)
     * @param
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value="/search/web/{page}/{size}",method= RequestMethod.POST)
    public Result searchPage(@PathVariable(value = "page") int page, @PathVariable int size){
        PageResult pageResult = articleService.searchPage(page, size);
        return new Result(true, StatusCode.OK,"查询成功",pageResult);
    }
    /**
     * 按分类分页查询(前台)
     * @param
     * @return
     */
    @RequestMapping(value="/search/web/category/{page}/{size}/{cid}",method= RequestMethod.GET)
    public Result searchByCategory(@PathVariable("page")int page, @PathVariable("size") int size, @PathVariable String cid){
        PageResult pageResult = articleService.searchByCategory(page, size, cid);
        return  new Result(true, StatusCode.OK,"查询成功",pageResult);
    }
    /**
     * 分页条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value="/search/{page}/{size}",method= RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
        Page<Article> pageList = articleService.findSearch(searchMap, page, size);
        return  new Result(true, StatusCode.OK,"查询成功",  new PageResult<Article>(pageList.getTotalElements(), pageList.getContent()) );
    }


    /**
     * 根据分类id集合和分类等级获取各分类下的文章数和文章id集合
     * @param level 分类等级
     * @param cidList 该等级下的所有分类id
     * @return
     */
    @PostMapping("/client/stats")
    public Result fetchTotalByCategory(@RequestParam int level, @RequestBody List<String>cidList){

        Map<String, Object> map = articleService.fetchTotalByCategory(level, cidList);
        return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,map);
    }

    @GetMapping("/client/sum")
    public Result getSum(){
       long num= articleService.sum();
        return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,num);

    }

    /**
     * 查询热门文章前20
     * @return
     */
    @PostMapping("/client/hot20")
    public Result fetchHotArticle(){
        System.out.println("经过了hot20");
        Map<String, Object> map = articleService.fetchHotArticle();
        return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,map);
    }



}
